package Server.lastproject;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.awt.HeadlessException;
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
 *
 * @author Admin
 */
public class ServerListen extends Thread {
    ServerForm parent;
  

    public ServerListen(ServerForm aThis) {
        parent = aThis;
    }

   
    @Override
    public void run()
    {
        int index =0 ;
        ThreadSocket[] thread = new ThreadSocket[200];
        ServerSocket ss = null;
        Boolean Continue = true;
        while(Continue)
        {
        try {
            Continue = false;
                JTextField PortField = new JTextField();
               PortField.setText("123");
                Object[] message = new Object[]{PortField};
                int r = JOptionPane.showConfirmDialog(parent, message, "Nhập cổng mà server lắng nghe!", JOptionPane.OK_CANCEL_OPTION);
                if (r == JOptionPane.OK_OPTION) { 
                    
                    String port = PortField.getText();
                    ss = new ServerSocket(Integer.parseInt(port));// Tao cong 9999 de server lang nghei
                    System.out.print("Start Listen in "+port);
                    while (true)// Cho client ket noi
                    {

                            thread[index] = new ThreadSocket(ss.accept());                  
                            index++;


                    }
            }

            
            
            
            } catch (HeadlessException | NumberFormatException | IOException ex) {
                int r = JOptionPane.showConfirmDialog(parent, "Cannot listen in port", "CLient", JOptionPane.YES_NO_OPTION);
                if (r == JOptionPane.YES_OPTION) {
                    Continue = true;
                }
              
        }
        
        }
            
        
       
        
    }
    
}
